package com.cat.dataStructure02;

import java.util.Arrays;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/maximum-alternating-sum-of-squares/
 * @create 2025/10/26 18:26
 * @since JDK17
 */

public class Solution63 {
    public long maxAlternatingSum(int[] nums) {
        int n = nums.length;
        for (int i = 0; i < n; i++) {
            nums[i] = Math.abs(nums[i]);
        }
        Arrays.sort(nums);
        long ans = 0;
        for (int i = 0, j = n - 1; i < j; i++, j--) {
            ans += (long) nums[j] * nums[j] - (long) nums[i] * nums[i];
        }
        if (n % 2 == 1) {
            ans += (long) nums[n / 2] * nums[n / 2];
        }

        return ans;
    }
}
